package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.mapper.EmployeeMapper;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping ("/admin/employee")
@Slf4j
@Api(tags = "员工管理接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录接口
     * 接收登录请求，验证用户身份并生成认证令牌
     *
     * @param employeeLoginDTO 登录请求数据传输对象，包含用户名和密码等信息
     * @return 登录结果封装对象，包含员工信息和 JWT 令牌
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);
        Employee employee = employeeService.login(employeeLoginDTO);
        // 创建存储 JWT 声明信息的 Map
        Map<String, Object> claims = new HashMap<>();
        // 将员工 ID 存入声明中
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        // 生成 JWT 令牌
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(), // 管理员 JWT 签名密钥
                jwtProperties.getAdminTtl(),       // 管理员 JWT 令牌有效期
                claims);                           // 令牌中携带的声明信息

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())                   // 员工 ID
                .userName(employee.getUsername())       // 员工用户名
                .name(employee.getName())               // 员工姓名
                .token(token)                           // 生成的 JWT 令牌
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 员工退出登录接口
     * 处理员工退出登录的请求
     *
     * @return 退出结果封装对象
     */
    @PostMapping("/logout")
    @ApiOperation(" 员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工接口
     *  employeeDTO
     * @return
     */

    @PostMapping("save")
    @ApiOperation("新增员工")
   public Result save(@RequestBody EmployeeDTO employeeDTO){
       log.info("新增员工");
       employeeService.save(employeeDTO);
       return Result.success();
   }

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public  Result<PageResult>page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启动禁用员工账户")
    public Result startOrStop(@PathVariable Integer status,long id){
        log.info("员工状态{}",status);
        employeeService.startOrstop(status,id);
        return Result.success();
    }

    /**
     *根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee>getByID(@PathVariable long id){
        log.info("根据id查询员工信息");
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     * @param employeeDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息");
        employeeService.update(employeeDTO);
        return Result.success();
    }
}


